﻿using Oracle.ManagedDataAccess.Client;
using System;
using WMSSyncData.DAL;
using WMSSyncData.DAL.MESTOWMS;
using WMSSyncData.DAL.SqlDB;
using WMSSyncData.Model;
using WMSSyncData.Utils;

namespace WMSSyncData.Core
{
    public class WMSSyncToERP
    {
        public string InStockSync()
        {
            Tddev108DAL tddev = new Tddev108DAL();
            InStockDAL inStockDAL = new InStockDAL();
            Tddev105DAL tddev105 = new Tddev105DAL();

            var list = tddev.GetSyncData();
            string msg = string.Empty;
            string bills = string.Empty;

            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    OracleConnection conn = OracleDBHelper.Create();
                    var tran = conn.BeginTransaction();
                    //check重复数据
                    var checkSql = "select t$wmso from ttddev108101 where t$wmso='{0}' and t$orno='{1}'";
                    checkSql = string.Format(checkSql, list[i].wmso, list[i].orno);
                    var dt = OracleDBHelper.GetDataSet(checkSql).Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        msg += list[i].wmso + "," + list[i].orno + "\r\n";
                        tddev.CloseDetail(list[i]);
                        tran.Rollback();
                        conn.Close();
                        continue;
                    }
                    //成品入库
                    var result = new BaseOracleDAL<tddev108>().Insert(conn, tran, list[i]);
                    if (result == 0)
                    {
                        tran.Rollback();
                        conn.Close();
                        continue;
                    }

                    //报工
                    var baogon = tddev105.GetSyncData(list[i].orno, list[i].wmso);
                    if (baogon.Count == 0)
                    {
                        //tran.Rollback();
                        //conn.Close();
                        //continue;
                    }
                    else
                    {
                        result = new BaseOracleDAL<tddev105>().InsertBatch(conn, tran, baogon);
                        if (result == 0)
                        {
                            tran.Rollback();
                            conn.Close();
                            continue;
                        }
                    }

                    var ret = tddev.CloseDetail(list[i]);
                    if (ret)
                    {
                        bills += "'" + list[i].wmso + "',";
                        tran.Commit();
                    }
                    else
                    {
                        tran.Rollback();
                    }
                    conn.Close();
                }
            }
            if (bills.Length > 0)
            {
                tddev.CloseMain(bills.Substring(0, bills.Length - 1));
            }
            return msg;
        }

        public void OutStockMSync()
        {
            OutStockMDAL outStockMDAL = new OutStockMDAL();
            var list = outStockMDAL.GetSyncData();
            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    OracleConnection conn = OracleDBHelper.Create();
                    var tran = conn.BeginTransaction();

                    var result = new BaseOracleDAL<tddev112>().Insert(conn, tran, list[i]);
                    if (result > 0 || result == -1)
                    {
                        //***20210221合并仓库
                        var adj = SH_SO_Adjust(list[i]);
                        if (adj != null)
                        { 
                            result = new BaseOracleDAL<tddev106>().Insert(conn, tran, adj);
                            if (result > 0 || result == -1)
                            {
                                outStockMDAL.UpdateUploadStatus(list[i]);
                                tran.Commit();
                                conn.Close();
                                continue;
                            }
                            else
                            {
                                tran.Rollback();
                                conn.Close();
                                continue;
                            }
                        }

                        outStockMDAL.UpdateUploadStatus(list[i]);
                        tran.Commit();
                        conn.Close();
                        continue;
                    }
                    else
                    {
                        tran.Rollback();
                        conn.Close();
                        continue;
                    }
                }
            }
        }

        public void UpdateLocationStatus()
        {
            string sql = @"update T_Bllb_StorageLocation_tbsl set Status_Flag='0' where Location_SN in (
select tbsl.Location_SN from T_Bllb_StorageLocation_tbsl tbsl 
inner join T_Bllb_StorageArea_tbsa tbsa on tbsl.Area_SN=tbsa.Area_SN
inner join T_Bllb_Storage_tbs tbs on tbsa.Storage_SN=tbs.Storage_SN where Storage_TYPE = '201'
and tbsl.Location_SN not in (SELECT DISTINCT Location_SN FROM wms_T_Inventory ivt
inner join T_Bllb_Storage_tbs tbs on ivt.Storage_SN=tbs.Storage_SN where Storage_TYPE = '201')
);
                           update T_Bllb_StorageLocation_tbsl set Status_Flag = '1' where Location_SN in(
                            SELECT DISTINCT Location_SN FROM wms_T_Inventory ivt
                            inner join T_Bllb_Storage_tbs tbs on ivt.Storage_SN=tbs.Storage_SN where Storage_TYPE = '201');";
            SqlDBHelper.ExcuteSQL(sql);
        }

        public tddev106 SH_SO_Adjust(tddev112 tdd)
        {
            tddev106 t106 = new tddev106();
            if (tdd.wrhs == "FGD3X" || tdd.wrhs == "FGD4X")
            {
                //移库对象
                t106.guid = tdd.wmso;
                t106.WMSNO = tdd.wmso;
                t106.orno = tdd.orno;
                t106.pono = 0;
                t106.seri = "WH5";
                t106.otyp = "TWH";
                t106.item = tdd.item;
                t106.revi = " ";
                t106.sfco = "FGWHS";
                t106.sflo = " ";
                t106.stco = tdd.wrhs;
                t106.stlo = " ";
                t106.qoor = Convert.ToDecimal(tdd.qoro);
                t106.host = 1;
                t106.mess = " ";
                t106.edte1 = tdd.edte1;
                t106.wdte1 = tdd.wdte1;
                t106.edte2 = tdd.edte1;
                t106.REFCNTD = 0;
                t106.REFCNTU = 0;
                t106.epon = 0;
                t106.seqn = 0;

                t106.Remark1 = " ";
                t106.Remark2 = " ";
                t106.Remark3 = " ";
                t106.Remark4 = " ";
                t106.Remark5 = " ";
            }
            else
            {
                return null;
            }
            return t106;
        }
        //END
    }
}
